#!/bin/bash

# Copyright (c) 2020 Teradici Corporation
#
# This source code is licensed under the MIT license found in the
# LICENSE file in the root directory of this source tree.

LOG_FILE="/var/log/teradici/user-data.log"
INSTALL_DIR="/root"

set -x

if [[ ! -f "$LOG_FILE" ]]
then
    mkdir -p "$(dirname $LOG_FILE)"
    touch "$LOG_FILE"
    chmod 600 "$LOG_FILE"
fi

exec &>> $LOG_FILE

echo "[$(date)] Running $0 as $(whoami)..."

cd $INSTALL_DIR

yum -y update
yum -y install dos2unix unzip

# Install AWS CLI directly since it is not in CentOS 8 Yum Repo
curl "https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip" -o "awscliv2.zip"
unzip awscliv2.zip
./aws/install

# Make aws available for root later
ln -s /usr/local/bin/aws /usr/bin/aws

aws s3 cp s3://${bucket_name}/${file_name} .
chmod +x ${file_name}
dos2unix ${file_name}
. ${file_name}

echo "[$(date)] $0 finished."
